import ChannelBase from "../base/ChannelBase";
import ChannelManager from "../manager/ChannelManager";

export default class VIVOChannel extends ChannelBase {

    /**
     * 初始化渠道
     */
    protected InitChannel() {
        this.is_has_ad = true;
        this.is_has_native = true;

        if (this.is_has_ad) this.CreateVideo();
        this.banner_height = 0;
    }

    //#region 广告相关
    private show_page_time: number = 0;
    private show_banner_time: number = 0;
    private is_first_load_video: boolean = true;

    /**
     * 创建插屏广告
     */
    public CreatePage() {

    }
    /**
     * 展示插屏广告
     */
    public ShowPage(on_successed: Function, on_failed: Function) {
        let time = this.GetCurrentTimestamp(false);
        //两次加载插屏广告的间隔不能小于10秒
        if (time > this.show_page_time + 10) {
            this.show_page_time = time;
            this.page_obj = qg.createInterstitialAd({
                posId: this.page_id_list[0]
            });

            this.page_obj.show().then(() => {
                if (on_successed) on_successed();
            }).catch((err) => {
                console.log('插屏广告展示失败', JSON.stringify(err));
                if (on_failed) on_failed();
            })

            this.page_obj.onClose(function () {

            })

            this.page_obj.onError(function (res) {
                console.error("插屏广告", res);
            });
        }
    }

    /**
     * 创建视频广告
     */
    public CreateVideo() {
        this.video_obj = qg.createRewardedVideoAd({
            posId: this.video_id_list[0]
        });

        this.video_obj.load();

        var self = this;

        this.video_obj.onClose((res) => {
            if (res.isEnded) {
                if (self.video_successed != null && self.video_successed != undefined) {
                    self.video_successed();
                }
            } else {
                if (self.video_failed != null && self.video_failed != undefined) {
                    self.video_failed();
                }
            }
        });

        this.video_obj.onLoad(function () {
            console.log("video is loaded!");
            if (self.is_first_load_video) return;
            self.video_obj.show();
        });

        this.video_obj.onError(res => {
            if (self.video_failed != null && self.video_failed != undefined) {
                self.video_failed();
            }
            ChannelManager.GetInstance().ShowToast("暂时没有视频了!")
            console.error("视频广告", res.errMsg + '----' + res.errCode);
        });
    }
    /**
     * 展示视频广告
     */
    public ShowVideo(on_successed: Function, on_failed: Function) {
        this.video_successed = on_successed;
        this.video_failed = on_failed;
        if (this.is_first_load_video) {
            this.video_obj.show();
        } else {
            this.video_obj.load();
        }
        this.is_first_load_video = false;
    }

    /**
     * 展示banner广告
     */
    public ShowBanner(on_successed: Function, on_failed: Function) {
        let self = this;
        let time = this.GetCurrentTimestamp(false);
        if (time > this.show_banner_time + 10) {
            this.show_banner_time = time;
            this.banner_obj = qg.createBannerAd({
                posId: self.banner_id_list[0],
                style: {}
            });

            this.banner_obj.onError(err => {
                console.log("banner广告加载失败", err);
            });

            this.banner_obj.show().then(() => {
                console.log(">>>>>>>>>>", "横幅广告展示完成");
                if (on_successed) on_successed();
            }).catch((err) => {
                console.log(">>>>>>>>>>", JSON.stringify(err));
                if (on_failed) on_failed();
            })
        }
    }
    /**
     * 隐藏banner广告
     */
    public HideBanner() {
        if (this.banner_obj != null && this.banner_obj != undefined) {
            this.banner_obj.destroy();
            this.banner_obj = null;
        }
    }

    /**
     * 创建盒子广告
     */
    public CreateBox() { }
    /**
     * 显示盒子广告
     */
    public ShowBox() { }
    /**
     * 隐藏盒子广告
     */
    public HideBox() { }

    /**
     * 创建积木广告
     */
    public CreateJuggle() { }
    /**
     * 显示积木广告
     */
    public ShowJuggle() { }
    /**
     * 隐藏积木广告
     */
    public HideJuggle() { }

    /**
     * 创建原生广告
     */
    public CreateNative() {
    }
    /**
    * 展示原生广告
    */
    public ShowNative(on_successed: Function, on_failed: Function) {
        let self = this;
        let time = this.GetCurrentTimestamp(false);
        this.native_obj = qg.createNativeAd({
            posId: this.native_id_list[0],
        });

        this.native_obj.onLoad(function (res) {
            if (res && res.adList) {
                if (on_successed) on_successed(res.adList[0], self.native_obj);
            } else {
                if (on_failed) on_failed();
            }
        })

        this.native_obj.onError(err => {
            if (on_failed) on_failed();
            console.log("原生广告加载异常", err);
        });
    }
    /**
    * 隐藏原生广告
    */
    public HideNative() {

    }
    //#endregion 广告相关

    //#region 分享/录屏相关 
    /*
     * 获取设备信息
     */
    public GetSystemInfo() {return qg.getSystemInfo({});}
    /**
     * 分享游戏
     */
    public ShareGame() { qg.share(); }

    /**
     * 开始录屏
     */
    public StartRecord() { }
    /**
     * 停止录屏
     */
    public StopRecord(on_stop: Function) { }
    /**
     * 分享录屏
     */
    public ShareRecord(on_successed: Function, on_failed: Function) { }
    //#endregion 分享/录屏相关

    /*
     * 短震动
     */
    public VibrateShort() { }
    /*
     * 长震动
     */
    public VibrateLong() { }

    /**
     * 打开另一个小程序
     * @param target 目标小程序appid
     * @param path 打开的页面路径
     * @param extraData 需要传递给目标小程序的数据
     * @param ON_SUCCESS 成功回调,返回参数res
     * @param ON_FAILED 失败回调,返回参数res
     * @param is_developer envVersion 的合法值是否为 develop(开发版)
     */
    public NavigateToMiniProgram(target: string, path: string, extraData: object, ON_SUCCESS: Function, ON_FAILED: Function, is_developer = false) {

    }

    //#region 生命周期
    public OnShow(callback: Function) {
        qg.onShow(callback);
    }
    public OnHide(callback: Function) {
        qg.onHide(callback);
    }
    public OffShow(callback: Function) {
        qg.offShow(callback);
    }
    public OffHide(callback: Function) {
        qg.offHide(callback);
    }
    public GetLaunchOptionsSync(): any {
        return qg.getLaunchOptionsSync();
    }
    public GetEnterOptionsSync(): any {
        return qg.getEnterOptionsSync();
    }
    public ExitMiniProgram(on_successed: Function, on_failed: Function, on_complete: Function) {
        qg.exitMiniProgram(on_successed, on_failed, on_complete);
    }
    //#endregion 生命周期
}
